<template>
  <div class="about">
    <h1>{{ id ? "编辑" : "新增" }}广告位</h1>
    <el-form @submit.native.prevent="save" label-width="80px">
      <el-form-item label="名称">
        <el-input v-model="model.name"></el-input
      ></el-form-item>
      <el-form-item label="广告">
      <el-button type="primary" icon="el-icon-plus" circle plain @click="model.items.push({})"></el-button>
        <el-row style="flex">
        <el-col :md="12" v-for="(item,index) in model.items" :key="index">
          <el-form-item label="跳转链接" style="width:20.5rem">
            <el-input v-model="item.url" ></el-input>
          </el-form-item>
          <el-form-item label="图片" style="margin-top:0.4rem"> 
      <el-upload
  class="avatar-uploader"  
  action="http://47.104.164.246/admin/api/upload"
  :show-file-list="false"
  :on-success="(res)=>{$set(item,'image',res.url)}">
  <img v-if="item.image" :src="item.image" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
          </el-form-item>
      
          <el-button type="danger" icon="el-icon-delete" circle @click="model.items.splice(index,1)" plain></el-button>
        </el-col>
      </el-row>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "AdvEdit",
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        items:[]
      },
    };
  },
  methods: {
    async save() {
      this.model.parent = null;
      let res;
      if (this.id) {
        res = await this.$http1.put(`rest/advs/${this.id}`, this.model);
      } else {
        res = await this.$http1.post("rest/advs", this.model);
      }
      console.log(res);
      this.$router.push("/advs/list");
      this.$message({ type: "success", message: "保存成功" });
    },
    async fech() {
      const res = await this.$http1.get(`rest/advs/${this.id}`);
      this.model = res.data;
    },
  },
  created() {
    this.id && this.fech();
  },
};
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 16rem;
    height: 9rem;
    line-height: 9rem;
    text-align: center;
  }
  .avatar {
    width: 16rem;
    height: 9rem;
    display: block;
  }
</style>